<template>
    <el-dialog :title="titleMap[mode]" v-model="visible" :width="500" destroy-on-close @closed="$emit('closed')" :close-on-click-modal="false" :close-on-press-escape="mode != 'show' ? false : true" align-center>
        <el-form :model="form" :rules="rules" :disabled="mode=='show'" label-width="100" ref="dialogForm">
            <el-form-item label="装柜费" prop="container_price">
                <el-input v-model="form.container_price" placeholder="请输入" type="Number" @mousewheel.prevent @input="total"></el-input>
            </el-form-item>
            <el-form-item label="其他" prop="otherc_price">
                <el-input v-model="form.otherc_price" placeholder="请输入" type="Number" @mousewheel.prevent @input="total"></el-input>
            </el-form-item>
            <el-form-item label="运营费用" prop="run_price">
                <el-input v-model="form.run_price" placeholder="请输入" type="Number" @mousewheel.prevent @input="total"></el-input>
            </el-form-item>
            <el-form-item label="合计杂费" prop="sundry_price">
                <el-input v-model="form.sundry_price" placeholder="请输入" readonly></el-input>
            </el-form-item>
        </el-form>
        <template #footer>
			<el-button @click="visible = false">取 消</el-button>
			<el-button v-if="mode != 'show'" type="primary" :loading="isSaveing" @click="submit(0)">保存</el-button>
        </template>
    </el-dialog>
</template>

<script>

export default {
    emits: ['success', 'closed'],
    data() {
        return {
            mode: "add",
            titleMap: {
                add: '新增加工单',
                edit: '编辑杂费',
                show: '查看加工单',
            },
            visible: false,
            isSaveing: false,
            form: {
                id: "",
                container_price: "",
                otherc_price: "",
                run_price: "",
                sundry_price: "",
            },
            lebel: "",
            prop: "",
        }
    },
    mounted() {
    },
    methods: {
        //显示
        open(mode = 'add') {
            this.mode = mode;
            this.visible = true;
            return this
        },
        //表单提交方法
        submit() {
            this.$refs.dialogForm.validate(async (valid) => {
                if (valid) {
                    this.isSaveing = true;
                    var res = await this.$API.ware.ware.send.updateFields.post(this.form);
                    this.isSaveing = false;
                    if (res.code == 1) {
                        this.visible = false;
                        this.$emit('success', this.form, this.mode)
                        this.$message.success("操作成功")
                    } else {
                        this.$alert(res.message, "提示", {type: 'error'})
                    }
                }
            })
        },
        //表单注入数据
        async setData(data) {
            this.form.id = data.id
            this.form.container_price = data.container_price || 0
            this.form.otherc_price = data.otherc_price || 0
            this.form.run_price = data.run_price || 0
            this.form.sundry_price = data.sundry_price || 0
        },
        total(){
            this.form.sundry_price = (Number(this.form.container_price)  + Number(this.form.otherc_price) + Number(this.form.run_price)).toFixed(2)
        },
    }
}
</script>

<style lang="scss" scoped>
.flex{
    display: flex;
    justify-content: space-between;
}
</style>
